package com.abel.quiz.common.vo;/**
 * Created by abel.lin on 2017/8/7.
 */

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author abel.lin
 * @date_time 2017/8/7 14:02
 * @description
 */
public class AjaxResp {
    private String code;
    private String msg;
    private long total = 0;
    private Object data;

    public AjaxResp(String code){
        this.code = code;
    }
    public AjaxResp(String code, String msg){
        this.code = code;
        this.msg = msg;
    }

    public void toWriter(HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().print(JSON.toJSONString(this));
    }

    public String toJson(){
        return JSON.toJSONString(this);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public static boolean isAjax(HttpServletRequest request){
        boolean isAjax = false;
        if(request != null){
            isAjax = request.getHeader("X-Requested-With") != null  && "XMLHttpRequest".equals( request.getHeader("X-Requested-With").toString());
        }
        return isAjax;
    }

    public static class CodeArgs{
        public static final String OK = "OK";  //成功
        public static final String NO = "NO";  //失败
    }
}
